package cn.shopping.controller.brand;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.shopping.pojo.Sh_brand;
import cn.shopping.service.brand.BrandService;


@Controller
@RequestMapping("/brand")
public class BrandController {

	@Resource
	private BrandService brandService;
	
	
	
	@RequestMapping(value="/brandlist.html",method=RequestMethod.GET)
	public String brandlist(Model model){
		List<Sh_brand> list =brandService.get_Brandlist();
		model.addAttribute("list", list);
		return "pinpailiebiao";
	}
	
	@RequestMapping(value="/del_brand.html",method=RequestMethod.GET)
	public String del_brand(@RequestParam Integer id,Model model){
		if (brandService.del_brand(id)==1) {
			model.addAttribute("error", "删除失败");
			return "redirect:/brand/brandlist.html";
		}
		return "redirect:/brand/brandlist.html";
	}
	
	
	@RequestMapping(value="/to_add_brand.html",method=RequestMethod.GET)
	public String to_add_brand(){
		return "shangchuanpinpai";
	}
	
	@RequestMapping(value="/add_brand.html")
	public String add_brand(
			Sh_brand brand,
			MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{
		String path = request.getSession().getServletContext().getRealPath("/images");  
		String fileName = file.getOriginalFilename();  
		File dir = new File(path, fileName);  
		 if(!dir.exists()){  
	            dir.mkdirs();  
	        } 
		 file.transferTo(dir);  
		
		return "shangchuanpinpai";
	}
	
	@RequestMapping(value="/add_brand.html",method=RequestMethod.POST)
	public String add_brand(
			Sh_brand brand,
			HttpSession session,
			HttpServletRequest request,
			@RequestParam(value="file",required=false) MultipartFile attach,Model model){
		String logoPath = null;
		if (!attach.isEmpty()) {
			String path = request.getSession().getServletContext().getRealPath("shoppingadmin"+"uploadfiles");
			String oldFileName = attach.getOriginalFilename();//原文件名
			String prefix = FilenameUtils.getExtension(oldFileName);//原文件后缀
			int finalize = 500000;
			if (attach.getSize()>finalize) {
				request.setAttribute("fileUploadError", "上传大小不得超过500kb");
				return "shangchuanpinpai";
			}else if(prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png") 
					   ||prefix.equalsIgnoreCase("jepg") || prefix.equalsIgnoreCase("pneg")){//上传图片格式
						String FileName = attach.getOriginalFilename();//原文件名
						 String fileName = System.currentTimeMillis()+FileName + ".jpg";//上传LOGO图片命名:apk名称.apk
						 File targetFile = new File(path,fileName);
						 if(!targetFile.exists()){
							 targetFile.mkdirs();
						 }
						 try {
							attach.transferTo(targetFile);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							request.setAttribute("fileUploadError", "上传失败");
							return "shangchuanpinpai";
						} 
						 logoPath = request.getContextPath()+"/statics/uploadfiles/"+fileName;
			}else {
				request.setAttribute("fileUploadError", "格式不正确");
				return "shangchuanpinpai";
			}
		}
		brand.setPicture(logoPath);
			if (brandService.add_brand(brand)==1) {
				return "redirect:/brand/brandlist.html";
			}
		
		return "shangchuanpinpai";
	
	}
	
}
